package main

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/go-redis/redis/v8"
	"strconv"
	"time"
)

var ctx = context.Background()

func main() {
	client := redis.NewClient(&redis.Options{
		Addr:     "100.100.142.112:6379",
		Password: "", // no password set
		DB:       0,  // use default DB
	})

	var err error
	key := "radar_target_test"

	score := float64(time.Now().UnixMilli())
	err = client.ZAdd(ctx, key, &redis.Z{score, "aaaaa"}).Err()
	if err != nil {
		fmt.Println("zadd failed: ", err)
	}

	// for i := 0; i < 20; i++ {
	// 	dataMap := make(map[string]interface{})
	// 	err := json.Unmarshal([]byte(targetData), &dataMap)
	// 	if err != nil {
	// 		fmt.Println("unmarshal failed: ", err)
	// 		continue
	// 	}

	// 	dataMap["timestamp"] = time.Now().UnixNano() / 1e6
	// 	byts, err := json.Marshal(dataMap)
	// 	if err != nil {
	// 		fmt.Println("Marshal failed: ", err)
	// 		continue
	// 	}
	// 	// err = client.ZAdd(ctx, key, &redis.Z{float64(time.Now().UnixNano() / 1e6), string(byts)}).Err()
	// 	err = client.ZAdd(ctx, key, &redis.Z{score, string(byts)}).Err()
	// 	if err != nil {
	// 		fmt.Println("zadd failed: ", err)
	// 		continue
	// 	}

	// 	fmt.Println("write data ", dataMap["timestamp"])
	// 	time.Sleep(time.Millisecond)
	// }

	/*time.Sleep(time.Minute)

	err = client.ZRemRangeByScore(ctx, key, "0", "("+strconv.Itoa(scoreInt)).Err()
	if err != nil {
		fmt.Println("delete failed: ", err)
		return
	}

	size, err := client.ZCard(ctx, key).Result()
	if err != nil {
		fmt.Println("zcard failed: ", err)
		return
	}
	fmt.Println("zcard size: ", size)

	end := int(time.Now().UnixNano() / 1e6)
	start := end - 20000000
	size, err = client.ZCount(ctx, key, strconv.Itoa(start), strconv.Itoa(end)).Result()
	// size, err = client.ZCount(ctx, key, strconv.Itoa(scoreInt), strconv.Itoa(scoreInt)).Result()
	if err != nil {
		fmt.Println("zcount failed: ", err)
		return
	}
	fmt.Println("ZCount size: ", size)*/

	// expire := client.Expire(ctx, key, 5*time.Minute)
	// err = expire.Err()
	// if err != nil {
	// 	fmt.Println("set expire failed", err)
	// 	return
	// }

	op := &redis.ZRangeBy{
		Min:    strconv.Itoa(int(score)), // 最小分数
		Max:    strconv.Itoa(int(score)), // 最大分数
		Offset: 0,                        // 类似sql的limit, 表示开始偏移量
		Count:  10000,                    // 一次返回多少数据
	}

	vals, err := client.ZRevRangeByScore(ctx, key, op).Result()
	if err != nil {
		fmt.Println("ZRevRangeByScore failed", err)
		return
	}

	for _, val := range vals {
		fmt.Println(val)
	}
}

const targetData = `{"data":[{"abnormal_state":0,"confidence":0,"is_virtual":true,"latitude":-15,"longitude":444.8,"object_cog":1.3639275,"object_id":"1645218880033","object_lane":85,"object_length":0,"object_sog":75.62142,"object_type":0,"object_x":448.00494,"object_y":-7.7346163},{"abnormal_state":0,"confidence":0,"is_virtual":true,"latitude":15,"longitude":-31.74276568889618,"object_cog":180,"object_id":"1645218885733","object_lane":115,"object_length":0,"object_sog":68.22326,"object_type":0,"object_x":-33.93381,"object_y":0},{"abnormal_state":0,"confidence":0,"is_virtual":true,"latitude":25,"longitude":-47.05115337371826,"object_cog":179.38667,"object_id":"1645218888433","object_lane":125,"object_length":0,"object_sog":84.077034,"object_type":0,"object_x":-50.918873,"object_y":0},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":-5,"longitude":201.67501831054688,"object_cog":0,"object_id":"1645218893832","object_lane":95,"object_length":0,"object_sog":95.399994,"object_type":0,"object_x":204.31487,"object_y":-9.199997},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":25,"longitude":28.240673065185547,"object_cog":177.51717,"object_id":"1645218890833","object_lane":125,"object_length":0,"object_sog":83.10214,"object_type":0,"object_x":25.94328,"object_y":5.199997},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":-15,"longitude":53.662513732910156,"object_cog":1.4502163,"object_id":"1645218898032","object_lane":85,"object_length":0,"object_sog":71.12278,"object_type":0,"object_x":55.62995,"object_y":-11.600006},{"abnormal_state":0,"confidence":0,"is_virtual":true,"latitude":-15,"longitude":363.13751220703125,"object_cog":1.5481577,"object_id":"1645218882633","object_lane":85,"object_length":0,"object_sog":66.62432,"object_type":0,"object_x":365.9424,"object_y":-8.714001},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":25,"longitude":112.7249984741211,"object_cog":180.77422,"object_id":"1645218892133","object_lane":125,"object_length":0,"object_sog":66.60608,"object_type":0,"object_x":110.88208,"object_y":5.800003},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":-15,"longitude":241.87503051757812,"object_cog":1.5481577,"object_id":"1645218887233","object_lane":85,"object_length":0,"object_sog":66.62432,"object_type":0,"object_x":243.71794,"object_y":-9.800003},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":15,"longitude":217.75003051757812,"object_cog":181.36392,"object_id":"1645218897933","object_lane":115,"object_length":0,"object_sog":75.62142,"object_type":0,"object_x":215.65807,"object_y":4.800003},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":15,"longitude":159.87503051757812,"object_cog":181.33221,"object_id":"1645218895033","object_lane":115,"object_length":0,"object_sog":77.42093,"object_type":0,"object_x":157.73326,"object_y":3.399994},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":-25,"longitude":235.2750244140625,"object_cog":1.9091524,"object_id":"1645218891433","object_lane":75,"object_length":0,"object_sog":81.04499,"object_type":0,"object_x":237.5164,"object_y":-13},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":-5,"longitude":133.87503051757812,"object_cog":0.5405102,"object_id":"1645218895533","object_lane":95,"object_length":0,"object_sog":95.40424,"object_type":0,"object_x":136.51488,"object_y":-8.600006},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":-15,"longitude":160.75003051757812,"object_cog":0,"object_id":"1645218893532","object_lane":85,"object_length":0,"object_sog":68.4,"object_type":0,"object_x":162.64276,"object_y":-12}],"device_id":"","radar_id":"test","time_interval":101,"time_str":"2022-19-02 05:14:59.233","timestamp":1645218899233}`
const targetData2 = `{"data":[{"abnormal_state":0,"confidence":0,"is_virtual":true,"latitude":-15,"longitude":444.8,"object_cog":1.3639275,"object_id":"1645218880033","object_lane":85,"object_length":0,"object_sog":75.62142,"object_type":0,"object_x":448.00494,"object_y":-7.7346163},{"abnormal_state":0,"confidence":0,"is_virtual":true,"latitude":15,"longitude":-31.74276568889618,"object_cog":180,"object_id":"1645218885733","object_lane":115,"object_length":0,"object_sog":68.22326,"object_type":0,"object_x":-33.93381,"object_y":0},{"abnormal_state":0,"confidence":0,"is_virtual":true,"latitude":25,"longitude":-47.05115337371826,"object_cog":179.38667,"object_id":"1645218888433","object_lane":125,"object_length":0,"object_sog":84.077034,"object_type":0,"object_x":-50.918873,"object_y":0},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":-5,"longitude":201.67501831054688,"object_cog":0,"object_id":"1645218893832","object_lane":95,"object_length":0,"object_sog":95.399994,"object_type":0,"object_x":204.31487,"object_y":-9.199997},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":25,"longitude":28.240673065185547,"object_cog":177.51717,"object_id":"1645218890833","object_lane":125,"object_length":0,"object_sog":83.10214,"object_type":0,"object_x":25.94328,"object_y":5.199997},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":-15,"longitude":53.662513732910156,"object_cog":1.4502163,"object_id":"1645218898032","object_lane":85,"object_length":0,"object_sog":71.12278,"object_type":0,"object_x":55.62995,"object_y":-11.600006},{"abnormal_state":0,"confidence":0,"is_virtual":true,"latitude":-15,"longitude":363.13751220703125,"object_cog":1.5481577,"object_id":"1645218882633","object_lane":85,"object_length":0,"object_sog":66.62432,"object_type":0,"object_x":365.9424,"object_y":-8.714001},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":25,"longitude":112.7249984741211,"object_cog":180.77422,"object_id":"1645218892133","object_lane":125,"object_length":0,"object_sog":66.60608,"object_type":0,"object_x":110.88208,"object_y":5.800003},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":-15,"longitude":241.87503051757812,"object_cog":1.5481577,"object_id":"1645218887233","object_lane":85,"object_length":0,"object_sog":66.62432,"object_type":0,"object_x":243.71794,"object_y":-9.800003},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":15,"longitude":217.75003051757812,"object_cog":181.36392,"object_id":"1645218897933","object_lane":115,"object_length":0,"object_sog":75.62142,"object_type":0,"object_x":215.65807,"object_y":4.800003},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":15,"longitude":159.87503051757812,"object_cog":181.33221,"object_id":"1645218895033","object_lane":115,"object_length":0,"object_sog":77.42093,"object_type":0,"object_x":157.73326,"object_y":3.399994},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":-25,"longitude":235.2750244140625,"object_cog":1.9091524,"object_id":"1645218891433","object_lane":75,"object_length":0,"object_sog":81.04499,"object_type":0,"object_x":237.5164,"object_y":-13},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":-5,"longitude":133.87503051757812,"object_cog":0.5405102,"object_id":"1645218895533","object_lane":95,"object_length":0,"object_sog":95.40424,"object_type":0,"object_x":136.51488,"object_y":-8.600006},{"abnormal_state":0,"confidence":0,"is_virtual":false,"latitude":-15,"longitude":160.75003051757812,"object_cog":0,"object_id":"1645218893532","object_lane":85,"object_length":0,"object_sog":68.4,"object_type":0,"object_x":162.64276,"object_y":-12}],"device_id":"","radar_id":"test","time_interval":101,"time_str":"2022-19-02 05:14:59.233","timestamp":1645218899266}`

// ConvByJson
func ConvByJson(src interface{}, dest interface{}) error {
	tmpbs, err := json.Marshal(src)
	if err != nil {
		return err
	}
	return json.Unmarshal(tmpbs, dest)
}
